Test data and Trace tables to document dry runs of algorithms
Objectives : Student should be able to -
Q1. a) Describe what is meant by Test data and state its purpose.
⇒ Test data is a set of data items used to input and check the algorithm written in pseudocode/flowchart by a person, or a program running on a computer to make sure that its solution does not give any incorrect results.
b) Describe the following four types of Test-data with example.
⇒ Data input within the acceptable limits of the program, that should be accepted to produce expected result without error is called Normal data.
Example : If the user is asked to enter a Mark between 0 and 50 (like, Marks >= 0 AND Marks <= 50 ), then 'Normal data' would be any value between 0 and 50, like -
Test-data : 35
✬ Abnormal /or/ Erroneous data :
⇒ Data input which is beyond the limits of the acceptable range of the program, which should be rejected and produce an error is called Abnormal data.
Example : If the user is asked to enter a Mark between 0 and 50 (like, Mark >= 0 AND Mark <= 50 ), then 'Abnormal data' would be, like -
✬ Extreme /or/ Borderline data :
⇒ Data input which is either Upper or Lower limit of the acceptable range of the program, that should be accepted to produce expected result without error is called Extreme data.
Example : If the user is asked to enter a Mark between 0 and 50 (like, Mark >= 0 AND Mark <= 50 ), then 'Extreme data' would be either 0 or 50, like -
Test-data : 0 /or/ 50
⇒ A pair of data input which are at each end of the set range of the program;
- The data at the upper or lower limits of acceptable range that should be accepted.
- The immediate values before or beyond the limits of acceptable range that should be rejected.
Example : If the user is asked to enter a number greater than 0, but less than or equal to 100 (like, Num > 0 AND Num <= 100 ), then 'Boundary data' would be like -
Test-data : 1 /or/ 100 (which should be accepted)
0 /or/ 101 (which should be rejected)
Q2. A programmer has written an algorithm to check that prices are less than $10.00
These values are used as test data :
10.00 9.99 ten
State why each value was chosen as test data.
10.00 ⇒ Boundary data, the price should be rejected because, it is the immediate value above the acceptable range.
9.99 ⇒ Extreme data, the price should be accepted because, it is the upper limit of the acceptable range.
ten ⇒ Abnormal data, the input should be rejected because, the value is of wrong data type.
Q3. Tickets are sold for a concert at $20 each. If 10 tickets are bought then the discount is 10%, if 20 tickets are bought the discount is 20%. No more than 25 tickets can be bought in a single transaction.
a) Use pseudocode to write the algorithm to calculate the cost of buying a given number of tickets.
REPEAT
OUTPUT "How many tickets would you like to buy?"
INPUT NumberOfTickets
UNTIL (NumberOfTickets > 0 AND NumberOfTickets < 26)
IF NumberOfTickets < 10 THEN
Discount ← 0
ELSE
IF NumberOfTickets < 20 THEN
Discount ← 0.1
ELSE
Discount ← 0.2
ENDIF
ENDIF
Cost ← NumberOfTickets * 20 * (1 - Discount)
PRINT "Your ticket cost is ", Cost
b) Explain how you would test your algorithm.
I would use test data with value of -
⇒ 0 and 26, immediate value beyond the acceptable range called boundary data, which should be rejected.
⇒ 1 and 25, lower and upper limit of the acceptable range called extreme data, which should be accepted to produce the result 20 and 400.
⇒ 9 and 10, one value without discount and another with discount of 10%, to produce the result 180 and 180.
⇒ 19 and 20, one value with discount of 10% and another with discound of 20%, to produce the result 342 and 320.
Q4. What are Trace table ? State the purpose of Trace table.
⇒ Trace-table is table that contains columns for each variable and a column of any output.
⇒ It is a tabular method to trace and record the value of variables as each line of code is dry run for a given set of data.
Dry run means that you don’t actually run the code in computer, you work through it on paper.The two main purpose of using Trace-table are -
- To determine, what a computer would do if the program were to execute.
- To test the logic of an algorithm, and find errors if any that are not easily spotted.
Q5. The pseudocode represents an algorithm.
The pre-defined function DIV gives the value of the result of integer division.
For example, Y = 9 DIV 4 gives the value Y = 2
The pre-defined function MOD gives the value of the remainder of integer division.
For example, R = 9 MOD 4 gives the value R = 1
First ← 0
Last ← 0
INPUT Limit
FOR Counter ← 1 TO Limit
INPUT Value
IF Value >= 100 THEN
IF Value < 1000 THEN
First ← Value DIV 100
Last ← Value MOD 10
IF First = Last THEN
OUTPUT Value
ENDIF
ENDIF
ENDIF
NEXT Counter
a) Complete the trace table for the algorithm using this input data :
8, 66, 606, 6226, 8448, 642, 747, 77, 121
Counter | Value | First | Last | Limit | Output |
0 | 0 | 8 | |||
1 | 66 | ||||
2 | 606 | 6 | 6 | 606 | |
3 | 6226 | ||||
4 | 8448 | ||||
5 | 642 | 6 | 2 | ||
6 | 747 | 7 | 7 | 747 | |
7 | 77 | ||||
8 | 121 | 1 | 1 | 121 | |
b) Describe the purpose of the algorithm.
⇒ Check and accept only three digit number; reject the number if it contains less than or greater than 3 digits.
⇒ Calculate and find the first and last digit; output the number only if they are same.
* * * * * * * * *
* * * * * *
* * *
*